package com.fy.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fy.core.vo.ApiRest;
import com.fy.sys.entity.DictData;
import com.fy.sys.entity.DictType;
import com.fy.sys.mapper.DictTypeMapper;
import com.fy.sys.service.DictTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典类型表 服务实现类
 * </p>
 *
 * @author hxd
 * @since 2023-06-14
 */
@Service
public class DictTypeServiceImpl extends ServiceImpl<DictTypeMapper, DictType> implements DictTypeService {


    @Override
    public ApiRest add(DictType dictType) {
        dictType.setStatus("0");
        dictType.setCreateTime(new Date());

        this.baseMapper.insert(dictType);

        return ApiRest.success("添加成功","");
    }

    @Override
    public List<DictType> findAll(String dictType) {

        return this.baseMapper.findAndTypeByDictType(dictType);
    }

    @Override
    public ApiRest updateByStatus(Long dictId) {

        DictType dictType = this.baseMapper.selectById(dictId);

        UpdateWrapper<DictType> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(DictType::getDictId,dictId);

        if (dictType.getStatus().equals("0")) {
            dictType.setStatus("1");
        }else {
            dictType.setStatus("0");
        }

        this.baseMapper.update(dictType,updateWrapper);

        return null;
    }

    @Override
    public ApiRest deleteDictType(Long dictId) {
        this.baseMapper.deleteById(dictId);
        return ApiRest.success("deleteSuccess","");
    }


    @Override
    public ApiRest batchDeleteType(List<DictType> dictTypes) {


        List<Long> codes = dictTypes.stream().map(item -> item.getDictId()).collect(Collectors.toList());

        for (Long code : codes) {
            this.baseMapper.deleteById(code);
        }

        return ApiRest.success("批量删除字典类型");
    }


}
